#!/usr/bin/perl
use CGI;
use CGI qw/:standard/;
use CGI::Pretty qw/:standard *-table *-div/;
use FindBin '$Bin';
use lib $Bin;
use DBI;
use Data::Dumper;
use CgiUtil;

my $g_task_id;
my $g_ex = 0;
my %g_table;

sub get_hold_data {
	my $content = "";
	my $db_name = "d_stock";
	my $db_host = "localhost";
	my $db_user = "root";
	my $db_pwd = "123456";
	my $dbh = DBI->connect("dbi:mysql:$db_name:$db_host", $db_user, $db_pwd);

        my $sql = "select c_date,c_hold,c_hold_ex,c_all from t_task_hold where c_task_id=$g_task_id";
        my $rows = $dbh->selectall_arrayref($sql);
	foreach (@$rows) {
		my ($date,$hold,$hold_ex,$all) = @$_;
		$g_table{"$date"}{"date"} = $date;
		$g_table{"$date"}{"hold"} = sprintf("%.2f",$hold);
		$g_table{"$date"}{"hold_ex"} = $hold_ex;
		$g_table{"$date"}{"all"} = sprintf("%.2f",$all);
	}
	#print Dumper(\%g_table);
	if ($g_ex == 1) {
		$content .= '<table border="1"><tr><th>日期</th><th>持仓价值</th><th>总价值</th><th>持仓明细</th></tr>';
		foreach my $key (sort keys %g_table) {
			$content .= "<tr><td>$g_table{$key}{'date'}</td>" . "<td>$g_table{$key}{'hold'}</td>" . "<td>$g_table{$key}{'all'}</td>" . "<td>$g_table{$key}{'hold_ex'}</td></tr>";
		}
	} else {
		$content .= '<table border="1"><tr><th>日期</th><th>持仓价值</th><th>总价值</th></tr>';
		foreach my $key (sort keys %g_table) {
			$content .= "<tr><td>$g_table{$key}{'date'}</td>" . "<td>$g_table{$key}{'hold'}</td>" . "<td>$g_table{$key}{'all'}</td></tr>";
		}
	}
	return $content;
}

sub main {
	print myhead("每日股值");
	print h3('每日股值');

	$g_task_id = url_param('task_id');
	$g_ex = url_param('ex');
	if (!defined($g_task_id)) {
		print h3("任务编号不正确");
	} else {
		print h3("任务编号:$g_task_id");
		print get_hold_data($g_task_id);
	}
}

main();
